<?php
namespace app\services;

use app\utils\HttpUtil as U;

class ApiService extends BaseService{
    public $adnetworkId = null;
    public $ssp = null; 
    public $username = null;
    public $password = null;
    public $conf = null;
    public $api = null;

    public function __construct($ssp) {
        parent::__construct();
        $this->ssp = $ssp; 
        $this->conf = $this->getConfInfo($ssp);
        $this->api = $this->conf[$ssp];
        $identify = $this->getSspIdentify($ssp);
        $this->username = $identify['username'];
        $this->password = $identify['password'];
    }

    public function getApiContent($url) {
        return U::getUrlContent($url, $this->username, $this->password);
    }

    public function post($url, $data) {
        return U::curl($url, $data, true, $this->username, $this->password);
    }

    public function getAdspaceInfo($api) {
        $url = $this->api[$api];
        $content = $this->getApiContent($url);
        return $content;
    }

    public function getSspIdentify($ssp) {
        $identify = ['username' => '', 'password' => ''];
        $SSPS = $this->getAll('AdNetwork', ['name' => $ssp]);
        if ($SSPS && !empty($SSPS)) {
            $ssp = array_pop($SSPS);
            $this->adnetworkId = $ssp['id']; //dsp 自增主键 id
            $identify['username'] = $ssp['account_name'];
            $identify['password'] = $ssp['account_password'];
        }
        return $identify;
    }

    public function getConfInfo($ssp) {
    	switch ($ssp) {
        case 'Acelink':
                $conf = \Yii::$app->params['accuen'];
                break;
        case 'vamaker':
                $conf = \Yii::$app->params['ssp'];
                break;
        default:
                $conf = \Yii::$app->params['ssp'];
                break;
        }
        return $conf;
    }

    public function formUrl($key) {
        return $this->api['domain'].$this->api[$key];
    }
}//eof
